<template>
  <div class="wrap">
    <!-- 注册内容 -->
    <div class="container">
      <h3>
        注册新用户
        <span class="go"
          >我有账号，去 <router-link to="/login">登陆</router-link>
        </span>
      </h3>
      <div class="center">
        <div class="content">
          <label for="phone">手机号:</label>

          <input
            v-validate="'required|phone'"
            :class="{ invalid: errors.has('phone') }"
            id="phone"
            placeholder="请输入你的手机号"
            v-model="phone"
            name="phone"
          />
          <span class="error-msg">{{ errors.first("phone") }}</span>
        </div>
        <div class="content">
          <label for="code">验证码:</label>

          <input
            v-validate="{ required: true, regex: /^\d{6}$/ }"
            :class="{ invalid: errors.has('code') }"
            placeholder="请输入你的验证码"
            v-model="code"
            name="code"
            id="code"
          />
          <button style="width: 100px; height: 38px" @click="getCode">
            获取验证码
          </button>
          <span class="error-msg">{{ errors.first("code") }}</span>
        </div>

        <div class="content">
          <label for="loginpass">登录密码:</label>
          <input
            v-validate="{ required: true, regex: /^[0-9A-Za-z]{8,20}$/ }"
            :class="{ invalid: errors.has('password') }"
            placeholder="请输入你的密码"
            v-model="password"
            name="password"
            id="loginpass"
          />
          <span class="error-msg">{{ errors.first("password") }}</span>
        </div>
        <div class="content">
          <label>确认密码:</label>
          <input
            placeholder="请输入确认密码"
            v-model="password1"
            name="password1"
            v-validate="{ required: true, is: password }"
            :class="{ invalid: errors.has('password1') }"
          />
          <span class="error-msg">{{ errors.first("password1") }}</span>
        </div>

        <div class="controls">
          <input
            type="checkbox"
            v-model="agree"
            name="agree"
            v-validate="'required|tongyi'"
          />
          <span>同意协议并注册《尚品汇用户协议》</span>
          <span class="error-msg">{{ errors.first("agree") }}</span>
        </div>
        <div class="btn">
          <button @click="userRegister">完成注册</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Register",

  data() {
    return {
      // 收集表单数据--手机号
      phone: "",
      //验证码
      code: "",
      //密码
      password: "",
      //确认密码
      password1: "",
      //是否同意
      agree: true,
    };
  },

  mounted() {},

  methods: {
    async getCode() {
      //简单判断一下---至少用数据
      try {
        //如果获取到验证码
        const { phone } = this;
        phone && (await this.$store.dispatch("user/getCode", phone));
        //将组件的code属性值变为仓库中验证码[验证码直接自己填写了]
        this.code = this.$store.state.user.code;
      } catch (error) {
        alert(error);
      }
    },
    async userRegister() {
      //全部表单验证成功，在向服务器发请求，进行祖册
      const success = await this.$validator.validateAll();
      //只要有一个表单没有成功，不会发请求
      if (success) {
        try {
          const { phone, code, password } = this;
          let userparams = {
            phone,
            code,
            password,
          };
          await this.$store.dispatch("user/userRegister", userparams);
          //注册成功进行路由的跳转
          this.$router.push("/login");
        } catch (error) {
          alert(error.message);
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.wrap {
  .container {
    height: 500px;
    border: 1px solid rgb(223, 223, 223);

    h3 {
      background: #ececec;

      padding: 6px 15px;
      color: #333;
      border-bottom: 1px solid #dfdfdf;
      font-size: 20.04px;
      line-height: 30.06px;

      span {
        font-size: 14px;
        float: right;

        a {
          color: #e1251b;
        }
      }
    }
    .center {
      position: relative;
      top: 50%;
      transform: translateY(-50%);
      div {
        margin-bottom: 30px;
      }
    }

    .content {
      padding-left: 360px;
      position: relative;

      label {
        font-size: 14px;
        width: 96px;
        text-align: right;
        display: inline-block;
        margin-right: 5px;
        vertical-align: middle;
      }
      input {
        width: 270px;
        height: 38px;
        padding-left: 8px;
        box-sizing: border-box;

        outline: none;
        border: 1px solid #999;
      }
      img {
        vertical-align: sub;
      }
      .error-msg {
        position: absolute;
        top: 100%;
        left: 495px;
        display: inline-block;
        margin-bottom: 10px;
        font-size: 16px;

        color: red;
      }
      button {
        text-align: center;
        line-height: 38px;
        margin: 0 17px;
        display: inline-block;
      }
    }

    .controls {
      text-align: center;

      position: relative;

      input {
        vertical-align: middle;
      }
      p {
        color: red;
      }
    }
    .btn {
    }
    button {
      width: 270px;
      height: 36px;
      margin: 0 auto;
      display: block;
      text-align: center;
      border: 1px solid #e1251b;
      outline: none;
      background-color: #e93854;
      color: #fff !important;

      font-size: 16px;
    }
  }
}
</style>
